<template>
  <div class="myContainer">
    <el-row class="myBorderBottom myMarginBottom_10px">
      <el-col :span="7" class="myTitleText">基本信息</el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        隶属考核类型 :
      </el-col>
      <el-col :span="6">
        <template>
          <el-select v-model="type_id"
            clearable
            :disabled="readonly"
            @change="changeType"
            placeholder="隶属考核类型">
            <el-option
              v-for="item in major"
              :key="item.type_name"
              :label="item.type_name"
              :value="item.type_id">
            </el-option>
          </el-select>
        </template>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        隶属考核项目 :
      </el-col>
      <el-col :span="6">
        <template>
          <el-select v-model="subtype_id"
            clearable
            :disabled="readonly"
            placeholder="隶属考核项目">
            <el-option
              v-for="item in subtypeOption"
              :key="item.subtype_id"
              :label="item.subtype_name"
              :value="item.subtype_id+'-'+item.subtype_name">
            </el-option>
          </el-select>
        </template>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        考核标准代码 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="考核标准代码" v-model="st_no" :readonly="readonly"></el-input>
      </el-col>
    </el-row>
    
    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        考核内容 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="考核内容" type="textarea" v-model="st_content" :readonly="readonly"></el-input>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        考核标准 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="考核标准" type="textarea" v-model="st_info" :readonly="readonly"></el-input>
      </el-col>
    </el-row>

    <!-- <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        不合格扣分值 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="不合格扣分值" v-model="st_value" :readonly="readonly"></el-input>
      </el-col>
    </el-row> -->

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        适用小区类型 :
      </el-col>
      <el-col :span="20" class="myLineHeight_40px">
        <template>
          <el-checkbox-group v-model="st_range">
            <el-checkbox label="住宅项目"></el-checkbox>
            <el-checkbox label="商业项目"></el-checkbox>
            <el-checkbox label="学校项目"></el-checkbox>
            <el-checkbox label="前期项目"></el-checkbox>
          </el-checkbox-group>
        </template>
      </el-col>
    </el-row>
    
    <el-row class="myBorderBottom myMarginBottom_10px">
      <el-col :span="7" class="myTitleText">状态及备注</el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
        启用状态 :
      </el-col>
      <el-col :span="6" class="myLineHeight_40px">
        <template>
          <el-radio-group v-model="status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </template>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        备注 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="备注" type="textarea" v-model="st_note" :readonly="readonly"></el-input>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="6" :offset="3">
        <el-button type="primary" v-if="type=='add'" @click="add">新增</el-button>
        <el-button type="primary" v-if="type=='update'" @click="update">保存</el-button>
        <el-button type="primary" @click="back">返回</el-button>
      </el-col>
    </el-row>
    
  </div>
</template>     
  
<style scoped>
  .myBorderBottom{
    border-bottom:1px dotted #dbdbdb;
    padding-bottom: 5px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleText{
    font-weight: 500;
    font-size: 16px;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myred{
    color: #f00;
    font-size: 12px;
  }
  .myMarginBottom_10px{
    margin-bottom: 10px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .mymarginTop_10px{
    margin-top: 10px;
  }
</style>

<script>
  import { addStandard , infoStandard , updateStandard , getTypeList , getSubtypeList } from '@/api/quality';
  export default {
    data(){
      return{
        title:"考核标准详情",
        type:"",
        id:"",
        readonly:false,
        major:[],
        st_no:"",
        type_id:"",
        st_content:"",
        st_value:"",
        status:1,
        st_range:[],
        st_info:"",
        st_note:"",
        subtype_id:'',
        subtypeOption:[]
      }
    },
    components: {
    },
    created(){
      this.type=this.$route.query.type;
      this.id=this.$route.query.id;
      this.ongetTypeList();
      // this.ongetSubtypeList();
      if(this.type=="add"){
        document.title="新增考核标准";
      }else if(this.type=="info"){
        this.readonly=true;
        this.info();
      }else if(this.type=="update"){
        document.title="编辑考核标准";
        this.readonly=false;
        this.info();
      }
      
    },
    computed: {
    },
    watch: { 
      type_id(){
        // this.subtype_id='';
        this.ongetSubtypeList(this.type_id);
      }
    },
    methods:{
      add(){
        let vm=this;
        let params={
          "st_no": vm.st_no,
          "type_id": vm.type_id,
          "subtype_id": vm.subtype_id.split('-')[0],
          "subtype_name": vm.subtype_id.split('-')[1],
          "st_content": vm.st_content,
          "st_value": Number(vm.st_value),
          "status": vm.status,
          "st_range": vm.st_range.toString(),
          "st_info": vm.st_info,
          "st_note": vm.st_note
        }
        addStandard(params).then(response => {
          vm.$message.success("执行成功！");
          vm.back();
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      update(){
        let vm=this;
        let params={
          "st_no": vm.st_no,
          "type_id": vm.type_id,
          "subtype_id": vm.subtype_id.split('-')[0],
          "subtype_name": vm.subtype_id.split('-')[1],
          "st_content": vm.st_content,
          "st_value": Number(vm.st_value),
          "status": vm.status,
          "st_range": vm.st_range.toString(),
          "st_info": vm.st_info,
          "st_note": vm.st_note
        }
        updateStandard(params,vm.id).then(response => {
          vm.$message.success("执行成功！");
          vm.back();
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      info(){
        let vm=this;
        infoStandard(vm.id).then(response => {
          let res=response.data; 
          vm.st_no=res.st_no;
          vm.type_id=res.type_id;
          vm.subtype_id=res.subtype_id+'-'+res.subtype_name;
          vm.st_content=res.st_content;
          vm.st_value=res.st_value;
          vm.status=res.status;
          vm.st_range=res.st_range.split(",");
          vm.st_info=res.st_info;
          vm.st_note=res.st_note;
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      ongetTypeList(){//类型列表
        let vm=this;
        let params={ 
          page_num:1,
          page_size:10000
        } 
        getTypeList(params).then(response => {
          let res=response.data;
          vm.major=res.rows; 
        }).catch(error => { 
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      back(){
        this.$router.go(-1);
      },
      ongetSubtypeList(type_id){
        let vm=this;
        let params={
          keyword:'',
          type_id:type_id,
          page_num:1,
          page_size:999
        }
        getSubtypeList(params).then(response => {
          let res=response.data;
          vm.subtypeOption=res.rows;
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      changeType(){
        this.subtype_id='';
        this.ongetSubtypeList(this.type_id);
      }
    }
}
</script>

